﻿using BusinessLogicLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WcfServiceLayer.ServiceContracts;
using ModelLayer;
using ModelLayer.Graph;
using ModelLayer.DTO;
using System.Threading;
using Microsoft.ApplicationServer.Http.Description;
using Newtonsoft.Json;
using Microsoft.ApplicationServer.Http.Activation;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.IO;
using System.Diagnostics;
using System.ServiceModel;

namespace WcfServiceLayer.Services
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
    class ClientService : IClientService
    {

        public RouteReservation GetById(string id)
        {
            throw new NotImplementedException();
        }



        public IList<BatteryType> GetAllBatteryTypes()
        {
            ReservationManager rm = new ReservationManager();
            return rm.GetAllBatteryTypes();
        }


        public IList<RouteReservation> GetUserReservations(string sessionToken)
        {
            ReservationManager rm = new ReservationManager();
            return rm.GetUserReservations(sessionToken);
        }

        public async Task<StationNetworkGraph> GetGraph(double[] startCoords, double[] endCoords, BatteryType desiredType, string sessionToken)
        {
            Debug.WriteLine("Got here");
            ReservationManager rm = new ReservationManager();
            return await rm.GetStationGraph(startCoords, endCoords, desiredType, sessionToken);
        }

        public PointReservationDTO sth()
        {
            return new PointReservationDTO() { StationId = 3, Distance = 55, ReservationTime = DateTime.Now };
        }

        public bool TryToReserveRoute(IList<PointReservationDTO> pointsToReserve, BatteryType desiredType, string sessionToken)
        {
            if(pointsToReserve.Count == 0)
            {
                return false;
            }

            ReservationManager rm = new ReservationManager();
            bool result = rm.TryToReserveRoute(pointsToReserve, desiredType, sessionToken);
            return result;

        }

        public Stream LogInUser(string username, string password)
        {
            AccountManager uam = new AccountManager();
            UserAccount account = uam.LogInUser(username, password);
            if(account != null)
            {
                string jsonClient = JsonConvert.SerializeObject(account);
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
                return new MemoryStream(Encoding.UTF8.GetBytes(jsonClient));
            }
            else
            {
                return null;
            }
        }

        public void ConfirmAccount(string sessionToken)
        {
            AccountManager uam = new AccountManager();
            uam.ConfirmAccount(sessionToken);

        }

        public bool SignUp(UserAccount accountToCreate)
        {
            AccountManager uam = new AccountManager();
            return uam.SignUserUp(accountToCreate);
        }


        public Stream GetAllAccounts()
        {
            AccountManager uam = new AccountManager();
            //Thread.Sleep(12000);
            //return uam.GetAllUserAccounts() ;
            string jsonClient = JsonConvert.SerializeObject(uam.GetAllUserAccounts());
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
            return new MemoryStream(Encoding.UTF8.GetBytes(jsonClient));
        }


        public BatteryType CheckBatteryType(BatteryType type)
        {
            return type;
        }
        #region Useless option functions that I have to create to make WCF happy

        public void GetOptionsGraph(){}

        public void GetOptionsSignUp(){}

        public void GetOptionsLogIn(){}

        public void GetOptionsTryToReserveRoute(){}

        public void GetOptionssth() { }

        #endregion




        
    }
}
